package com.yunc.modules.chat.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrSpliter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.CommonController;
import com.yunc.common.utils.R;
import com.yunc.modules.chat.entity.ChatGroups;
import com.yunc.modules.chat.service.IChatGroupsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户群表 前端控制器
 *
 * @author concong tao
 * @since 2019-03-28
 */
@RestController
@RequestMapping("/chat/groups")
public class ChatGroupsController extends CommonController {

    @Autowired
    private IChatGroupsService chatGroupsService;

    @GetMapping("/page")
    public R page(Page<ChatGroups> page, ChatGroups chatGroups){
        chatGroupsService.page(page, new QueryWrapper<>(chatGroups));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable Long id){
        ChatGroups chatGroups = chatGroupsService.getById(id);
        return R.ok().put("chatGroups", chatGroups);
    }

    @PostMapping("/save")
    public R save(@RequestBody ChatGroups chatGroups){
        boolean flag = chatGroupsService.save(chatGroups);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @PutMapping(value = "/update")
    public R update(@RequestBody ChatGroups chatGroups){
        boolean flag = chatGroupsService.updateById(chatGroups);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable String ids){
        if (StringUtils.isEmpty(ids)) {
            return R.error("传入值为空");
        }
        List<String> idsList = StrSpliter.split(ids, ',', 0, true, true);
        if (CollUtil.isEmpty(idsList)) {
            return R.error("传入值格式错误");
        }
        boolean flag = chatGroupsService.removeByIds(idsList);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
